import { Provide, Get, Query, ALL, Headers, Inject } from '@midwayjs/decorator';
import { CoolController, BaseController } from '@cool-midway/core';
import { MagicEntity } from '../../entity/magic';
import { MagicService } from '../../service/magic';
/**
 * 商品
 */
@Provide()
@CoolController({
  // 添加通用CRUD接口
  api: ['info'],
  // 设置表实体
  entity: MagicEntity,
  service: MagicService,
})
export class AppMagicController extends BaseController {
  @Inject()
  magicSer: MagicService;
  /**
   * @param id 魔法页面id
   * @returns 魔法页面数据
   */
  @Get('/infos')
  async pageInfo(@Query(ALL) query: any, @Headers(ALL) headers: any) {
    console.log('query--->', query);

    if (query.key) {
      return this.ok(
        await this.magicSer.getPageInfoByKey({
          key: query.key,
          source: headers.source,
        })
      );
    } else if (query.id) {
      return this.ok(
        await this.magicSer.getPageInfoById({
          id: query.id,
          source: headers.source,
        })
      );
    } else {
      return this.fail();
    }
  }

  @Get('/cate')
  async cateList(@Query(ALL) query: any, @Headers(ALL) headers: any) {
    console.log('query--->', query);

    return this.ok(await this.magicSer.getCateList({ source: headers.source }));
  }
}
